package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.PublishAnnounceListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.KnowledgeAnnounceList;
import com.isunland.managesystem.entity.KnowledgeAnnounceList.KnowledgeAnnounceDetail;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 发布公告列表
 *
 * @author wu
 */
public class PublishAnnounceListFragment extends BaseListFragment {

  private static final int REQUEST_EDIT = 0;
  private static final int REQUEST_ADD = 1;
  private static final int REQUEST_SEARCH = 2;
  private PublishAnnounceListAdapter mAdapter;
  private CurrentUser mCurrentUser;

  private String dataStatusCode;
  private String dataStatusName;
  private String beginregDate;
  private String endregDate;
  private String docKindCode;
  private String docKindName;
  private String docKeyWords;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.queryAnnounce);
    mCurrentUser = CurrentUser.newInstance(getActivity());
    dataStatusCode = "";
    dataStatusName = "全部";
    beginregDate = MyDateUtil.getBeforeThreeMonth();
    endregDate = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE);
    docKindCode = "";
    docKindName = "";
    docKeyWords = "";
  }

  @Override public String getUrl() {
    return ApiConst.URL_ANNOUNCE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<>();

    params.put("curPage", "1");
    params.put("pageSize", "1000");
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("regStaffId", mCurrentUser.getJobNumber());
    params.put("isPaging", "1");
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("docKindSymbol", "ZSLB-QYGG");
    params.put("docKindFlag", "ZSLB-QYGG");
    params.put("beginregDate", beginregDate);
    params.put("endregDate", endregDate);
    params.put("dataStatus", dataStatusCode);//状态
    params.put("docKindCode", docKindCode);//公文类别code
    params.put("docKeyWords", docKeyWords);//关键字
    params.put("systemIndex", "5");//固定值
    params.put("dataStyle", "1");//数据创建方式  1：期初管理  2 ：企业公告
    return params;
  }

  @Override public void resolveResponse(String json) {
    KnowledgeAnnounceList list = new Gson().fromJson(json, KnowledgeAnnounceList.class);
    ArrayList<KnowledgeAnnounceDetail> detail = (ArrayList<KnowledgeAnnounceDetail>) list.getRows();
    setAdapter(detail);
  }

  private void setAdapter(ArrayList<KnowledgeAnnounceDetail> detail) {

    if (detail == null) {
      detail = new ArrayList<>();
    }

    mAdapter = new PublishAnnounceListAdapter(mActivity, detail);
    setListAdapter(mAdapter);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    KnowledgeAnnounceDetail detail = mAdapter.getItem(position - 1);

    Intent intent = new Intent(getActivity(), PublishAnnounceDetailActivity.class);
    intent.putExtra(PublishAnnounceDetailFragment.EXTRA_VALUE, detail);
    startActivityForResult(intent, REQUEST_EDIT);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add_search, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override // 响应返回图标
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), PublishAnnounceActivity.class);
        startActivityForResult(intent, REQUEST_ADD);
        break;
      case R.id.menu_item_querys:
        //查询
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(dataStatusName);
        queryConten.setExtCode1(dataStatusCode);
        queryConten.setExtName2(beginregDate);
        queryConten.setExtName3(endregDate);
        queryConten.setExtName4(docKindName);
        queryConten.setExtCode4(docKindCode);
        queryConten.setExtName5(docKeyWords);
        Intent query = new Intent(getActivity(), PublishAnnounceQueryActivity.class);
        query.putExtra(PublishAnnounceQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_SEARCH);

        break;
      default:
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_EDIT || requestCode == REQUEST_ADD) {
      volleyPost();
    }
    if (requestCode == REQUEST_SEARCH) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(PublishAnnounceQueryFragment.EXTRA_CONTENT);
      dataStatusName = queryConten.getExtName1();
      dataStatusCode = queryConten.getExtCode1();
      beginregDate = queryConten.getExtName2();
      endregDate = queryConten.getExtName3();
      docKindName = queryConten.getExtName4();
      docKindCode = queryConten.getExtCode4();
      docKeyWords = queryConten.getExtName5();
      volleyPost();
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
}
